package sa.com.sure.ejtimaa.database;


import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.model.AttachmentInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class Attachments {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "Attachments";
	
    public static final String AttachmentIDField = "AttachmentID"; 
    public static final String FileNameField = "FileName";
    public static final String FileSizeField = "FileSize";
    public static final String FileTypeField = "FileType";
    public static final String FileContentField = "FileContent";
    
    
    private String[] allColumns = { AttachmentIDField, FileNameField,FileSizeField,FileTypeField,FileContentField };
    
    Cursor cursor = null;
    
    public Attachments(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
        
    public static Attachments Instance = new Attachments(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		if(cursor!=null && !cursor.isClosed())
			cursor.close();
		dbHelper.close();
	}
		
	
	public AttachmentInfo GetItem(String AttachmentID) throws SQLException
	{	
		try	
		{
			open();
		
			AttachmentInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, AttachmentIDField + "='" + AttachmentID +"'",null,null,null,null);
        	if (cursor.moveToFirst()) 	        	
        		item =  getItemInfo(cursor);	     
    	        
        	return 	item;
        	
		}
        finally{        	
            close();
        }    		        	   
	}
		
	
	public void addItem(AttachmentInfo item) 
	{ 
		try	
		{        
			open();
			
	        ContentValues values = new ContentValues(); 
	        
	        values.put(AttachmentIDField, item.GetAttachmentID());  
	        values.put(FileNameField, item.GetFileName());  
	        values.put(FileSizeField, item.GetFileSize());
	        values.put(FileTypeField, item.GetFileType());
	       // values.put(FileContentField, item.GetFileContent());
	         
	        dbHelper.db.insert(TABLE_Name, null, values); 
        
		}
	    finally{        	
	        close();
	    }  
                
    } 
	
	 public boolean updateItem(AttachmentInfo item) 
	 {
		 try	
			{		 
			 	open();
			 	
			    ContentValues values = new ContentValues();		    
			    
		        values.put(FileNameField, item.GetFileName());  
		        values.put(FileSizeField, item.GetFileSize());
		        values.put(FileTypeField, item.GetFileType());
		        //values.put(FileContentField, item.GetFileContent());
			    
			    boolean res =   dbHelper.db.update(TABLE_Name, values, AttachmentIDField + "='" + item.GetAttachmentID() + "'", null) > 0;			    			   
			    
			    return res;	        
			}
		    finally{        	
		        close();
		    }  	               
	 }
	
	
    private AttachmentInfo getItemInfo(Cursor cursor) {  
    	
    	AttachmentInfo itemInfo = new AttachmentInfo();
    	
    	itemInfo.SetAttachmentID(cursor.getString(cursor.getColumnIndex(AttachmentIDField)));
    	itemInfo.SetFileName(cursor.getString(cursor.getColumnIndex(FileNameField)));     	
    	itemInfo.SetFileSize(cursor.getInt(cursor.getColumnIndex(FileSizeField)));
    	itemInfo.SetFileType(cursor.getString(cursor.getColumnIndex(FileTypeField)));    	
    	//itemInfo.SetFileContent(cursor.isNull(cursor.getColumnIndex(FileContentField))?null:cursor.getBlob(cursor.getColumnIndex(FileContentField))); 
    	
		return itemInfo;
	}
}
